<template>
	<view>
		<picker class='time-picker' mode="multiSelector" @change="bindStartMultiPickerChange" @columnchange="bindMultiPickerColumnChange"
		  :value="multiIndex" :range="multiArray">
			<slot name="pCon"></slot>
		</picker>
	</view>
</template>

<script>
	export default {
		name: 'timePicker',
		props:{
			sTime: {
				type:String,
				default:"0"
			},
			cTime: {
				type:String,
				default:"23"
			},
			interval:{
				type:String,
				default:"1"
			}
		},
		data() {
			return {
				multiArray: [
				
				],
				multiIndex: [0, 0, 0],
				multiSelector: '',
			};
		},
		beforeMount() {
			this.pickerTap()
		},
		methods: {
			timeFormat: function(num) {

				if (num < 10 && (num + '').length == 1) {
					return '0' + num;
				}
				return num;
			},
			pickerTap: function() {
				let date = new Date();

				let monthDay = [];
				let hours = [];
				let minute = [];

				// 月-日
				for (let i = 0; i <= 15; i++) {
					let date1 = new Date(date);
					date1.setDate(date.getDate() + i);
					let md = date1.getFullYear() + '-' + this.timeFormat(date1.getMonth() + 1) + "-" + this.timeFormat(date1.getDate());
					monthDay.push(md);
				}

				// 时
				let date1 = new Date(date);

				let sT = +this.sTime;
				let eT = +this.cTime;
				let inter = +this.interval<60?+this.interval:59;
				if (sT <= eT) {
					let h = date1.getHours() < sT ? sT : date1.getHours();
					if (h > eT) {
						h = eT
					}
					for (let i = h; i <= eT; i++) {
						hours.push(this.timeFormat(i) + '时');
					}
				} else {
					let h = date1.getHours() < sT ? sT : date1.getHours();

					for (let i = h; i <= 23; i++) {
						if (i < sT && i > eT) {

						} else {
							hours.push(this.timeFormat(i) + '时');
						}
					}
				}

				// 分
				for (let i = 0; i < 60; i += inter) {

					minute.push(i < 10 ?'0' +  i + '分' : i + '分');
				}

				let data = {
					multiArray: this.multiArray,
					multiIndex: this.multiIndex
				};
				
				data.multiArray[0] = monthDay;
				data.multiArray[1] = hours;
				data.multiArray[2] = minute;
				this.multiArray = data.multiArray;
				this.multiIndex = data.multiIndex;
			},
			bindMultiPickerColumnChange(e) {
				// console.log(e);
				let hours = [];
				if (e.detail.column == 0 && e.detail.value == 0) {
					let date = new Date();
					let date1 = new Date(date);
					let sT = +this.sTime;
					let eT = +this.cTime;

					if (sT <= eT) {
						let h = date1.getHours() < sT ? sT : date1.getHours();
						if (h > eT) {
							h = eT
						}
						for (let i = h; i <= eT; i++) {
							hours.push(this.timeFormat(i) + '时');
						}
					} else {
						let h = date1.getHours() < sT ? sT : date1.getHours();

						for (let i = h; i <= 23; i++) {
							if (i < sT && i > eT) {

							} else {
								hours.push(this.timeFormat(i) + '时');
							}
						}
					}
					this.multiArray.splice(1, 1, hours)
				} else if (e.detail.column == 0 && e.detail.value != 0) {
					let sT = +this.sTime;
					let eT = +this.cTime;

					if (sT <= eT) {
						for (let i = sT; i <= eT; i++) {
							hours.push(this.timeFormat(i) + '时');
						}
					} else {
						for (let i = 0; i <= 23; i++) {
							if (i < sT && i > eT) {

							} else {
								hours.push(this.timeFormat(i) + '时');
							}
						}
					}
					this.multiArray.splice(1, 1, hours)
				}
				// console.log(this.multiArray);
			},
			bindStartMultiPickerChange(e) {
				this.multiIndex = e.detail.value;
			    let da = this.multiArray;
			    let di = e.detail.value;
			
			    let caseDate = da[0][di[0]] + ' ' + da[1][di[1]].replace('时', ':') + this.timeFormat( da[2][di[2]].replace('分', '')) + ":00"
			    // console.log(caseDate)
		
			    let appointTime = new Date(caseDate).getTime() / 1000;
			
			    if (appointTime < new Date().getTime() / 1000) {
			      uni.showToast({
			        title: '不能选择过去时间',
			        icon: 'none'
			      })
			      return false;
			    }
			    this.$emit('changeTime',caseDate,appointTime*1000)
			  },
		}
	}
</script>

<style lang="scss">

</style>
